package core;

import java.security.MessageDigest;

public class Tool {
	public static String implode(String split, Object[] obj){
		StringBuffer buf = new StringBuffer();
		for(int i = 0; i < obj.length - 1; i++){
			buf.append(obj[i].toString());
			buf.append(split);
		}
		buf.append(obj[obj.length - 1].toString());
		return buf.toString();
	}
	
	public static int intval(String str){
		if(str == null || str.isEmpty() || !str.matches("\\-{0,1}[0-9]+")){
			return 0;
		}
		return Integer.parseInt(str);
	}
	
	public static String htmlspecialchars(String html){
		if(html == null || html.isEmpty()){
			return "";
		}
		
		html = html.replaceAll("'", "&apos;");
		html = html.replaceAll("\"", "&quot;");
		html = html.replaceAll("\t", "&nbsp;&nbsp;&nbsp;&nbsp;");
		html = html.replaceAll("<", "&lt;");
		html = html.replaceAll(">", "&gt;");
		
		return html;
	}
	
	public static String addslashes(String str){
		if(str == null || str.isEmpty()){
			return "";
		}
		
		str = str.replaceAll("[^\\](\\\")", "\\\"");
		str = str.replaceAll("[^\\](\\')", "\\'");

		return str;
	}

	public static String md5(String str) {
		str += Config.encryptKey;
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			byte[] array = md.digest(str.getBytes());
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < array.length; ++i) {
				sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100)
						.substring(1, 3));
			}
			return sb.toString();
		} catch (java.security.NoSuchAlgorithmException e) {
		}
		return null;
	}
}
